package com.lingnan.common.utils;

import io.swagger.annotations.ApiModelProperty;
import lombok.Data;

import java.util.HashMap;
import java.util.Map;

/**
 * @author wweca
 * 统一结果数据格式
 */
@Data
public class ResultEntity {
    @ApiModelProperty(value = "是否成功")
    private Boolean success;

    @ApiModelProperty(value = "返回码")
    private Integer code;

    @ApiModelProperty(value = "返回消息")
    private String message;

    @ApiModelProperty(value = "返回数据")
    private Map<String, Object> data = new HashMap<String, Object>();

    /**
     * 构造方法私有
     */
    private ResultEntity() {}

    /**
     * 响应成功静态方法
     * @return
     */
    public static ResultEntity ok() {
        ResultEntity r = new ResultEntity();
        r.setSuccess(true);
        r.setCode(ResultCode.SUCCESS);
        r.setMessage("成功");
        return r;
    }

    /**
     * 响应失败静态方法
     * @return
     */
    public static ResultEntity error() {
        ResultEntity r = new ResultEntity();
        r.setSuccess(false);
        r.setCode(ResultCode.FAILED);
        r.setMessage("失败");
        return r;
    }

    /**
     * 返回操作成功还是失败
     */
    public ResultEntity success(Boolean success){
        this.setSuccess(success);
        return this;
    }

    /**
     * 返回数据
     */
    public ResultEntity message(String message){
        this.setMessage(message);
        return this;
    }

    /**
     * 返回状态码
     */
    public ResultEntity code(Integer code){
        this.setCode(code);
        return this;
    }

    /**
     * 返回数据（对象）
     * @param key
     * @param value
     * @return
     */
    public ResultEntity data(String key, Object value){
        this.data.put(key, value);
        return this;
    }

    /**
     * 返回数据（集合）
     * @param map
     * @return
     */
    public ResultEntity data(Map<String, Object> map){
        this.setData(map);
        return this;
    }

}
